package com.woniuxy.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.CarExcelListener;
import com.woniuxy.common.Result;
import com.woniuxy.dto.CarDto;
import com.woniuxy.entity.Car;
import com.woniuxy.entity.CarType;
import com.woniuxy.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;

    //分页插件的设置
    @PostMapping("/findPage")
    public Result<PageInfo<CarDto>> findPage(@RequestBody Map<String, Object> params) {
        //1.获取查询车辆，页码，页大小
        String carNumber = (String) params.get("carNumber");
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        //2.调用业务层
        PageInfo<CarDto> pageInfo = carService.findByPage(carNumber, pageNum, pageSize);
        //3.返回结果
        return Result.success(pageInfo);
    }
    //
    @GetMapping("/isCarNumberExists")
    public Result<Boolean> isCarNumberExists(String carNumber) {
        boolean result = carService.findByCar(carNumber) != null;
        return Result.success("车牌号已经存在", result);
    }

    /**
     * 添加
     */
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody Car car) {
        int row = carService.save(car);
        return Result.success("成功添加车牌号", row);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody Car car) {
        int row = carService.updateById(car);
        return Result.success("成功修改车牌号", row);
    }
    /**
     * 删除
     */
    @GetMapping("/deleteById")
    public Result<Integer> deleteById(Integer id){
        int row = carService.deleteById(id);
        return Result.success("成功删除车牌号", row);
    }
    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public Result<Integer> deleteBatch(@RequestBody String[] ids) {
        int row = carService.deleteBatch(ids);
        return Result.success("批量删除车牌号成功", row);
    }

    /**
     * 导出数据Excel
     */
    @GetMapping("/export")
    public void exportData(HttpServletResponse response) throws IOException {
        //1.调用业务层获取要导出数据
        List<CarDto> list = carService.findAll();
        //2.设置响应头，指定下载文件
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //文件名不使用汉字，否则需要进行编码，文件名会有乱码。告诉浏览器，这个文件要下载，并且指定文件名
        response.setHeader("content-disposition","attachment;filename=car.xlsx");
        //3.调用EasyExcel的方法导出文件
        //获取响应输出流
        ServletOutputStream out = response.getOutputStream();
        //参数：输出流，实体类
        EasyExcel.write(out,CarDto.class)
                .sheet("车辆管理")   //工作簿名字
                .doWrite(list);  //要写出的数据列表
    }

    /**
     * 导入Excel
     */
    @PostMapping("/import")
    public ResponseEntity<String> importData(MultipartFile file) throws IOException {
        //获取输入流
        InputStream inputStream = file.getInputStream();
        //创建Excel的监听器
        CarExcelListener listener = new CarExcelListener(carService);
        //参数：输入流，实体类，监听器对象
        EasyExcel.read(inputStream,CarDto.class,listener).sheet().doRead();
        return ResponseEntity.ok("成功导入" + listener.getRowCount() + "条记录");
    }

    //远程加载找车架号
    @RequestMapping("/findByFrameNumber")
    public Result<List<Car>> findByFrameNumber(String frameNumber) {
        List<Car> frameNumbers = carService.findAllFrameNumber(frameNumber);
        return Result.success(frameNumbers);
    }
    //远程加载找车架号
    @RequestMapping("/findByCarNumber")
    public Result<List<Car>> findByCarNumber(String carNumber) {
        List<Car> carNumbers = carService.findAllCarNumber(carNumber);
        return Result.success(carNumbers);
    }
    //查找所有车辆绑定id用
    @RequestMapping("/findCarList")
    public Result<List> findCarList() {
        List<Car> listCar = carService.findListCar();
        return Result.success(listCar);
    }
}
